// 代取
var express = require('express');
var router = express.Router();
var db = require('../model/index');

/* GET  */
router.get('/takeList', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM takeList`
    db(sql, null, function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
router.get('/takeImg', function (req, res) {
    //   res.send('respond with a resource');
    var sql = `SELECT * FROM takeImg where takeId = ?`
    db(sql, [req.query.takeId], function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物留言
router.get('/updateTake', function (req, res) {
    var sql = `INSERT into takemessage (leaveMessage,takeId,userId,leaveUser) VALUES (?,?,?,?);UPDATE takelist SET leaveMessageAudit=1 WHERE takeId = ?`
    db(sql, [req.query.leaveMessage, req.query.takeId, req.query.userId, req.query.leaveUser, req.query.takeId], function (err, data) {
        //    db(sql,null,function(err,data){

        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物举报
router.get('/jubaoTake', function (req, res) {
    var sql = `INSERT into takejubao (jubao,takeId,userId) VALUES (?,?,?);UPDATE takelist SET takeAudit=2 WHERE takeId = ?`
    db(sql, [req.query.jubao, req.query.takeId, req.query.userId, req.query.takeId], function (err, data) {
        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 寻物添加
router.post('/addTake', function (req, res) {
    console.log(req.body);
    var sql = `INSERT into takelist (InfoPublisher,takeAudit,
                takeContent,
                takeDesc,
                takeImg,
                takeTags,
                takeTitle,
                takeType,
                takeValidDate,
            leaveMessageAudit) VALUES (?)
            `

    let params = [req.body.InfoPublisher, req.body.takeAudit,
    req.body.takeContent,
    req.body.takeDesc,
    req.body.takeImg,
    // req.body.barterTagType,
    req.body.takeTags,
    req.body.takeTitle,
    req.body.takeType,
    req.body.takeValidDate,
    req.body.leaveMessageAudit]

    db(sql, [params], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 寻物图片批量
router.post('/addTakeImgs', function (req, res) {
    let arr = req.body.takeImgs; // 前端提交过来的数据
    console.log(arr, 11);
    let sql = `INSERT INTO takeimg (takeId,takeImg) VALUES(?,?)`    // 插入语句
    for (let i = 0; i < arr.length; i++) {
        db(sql, [req.body.takeId, arr[i]], function (err, data) {
            if (err) {
                console.log(err)
                if (i == arr.length - 1) {
                    res.send({
                        code: 500,
                        msg: "添加失败!",
                    });
                }
            } else {
                console.log(data);
                if (i == arr.length - 1) {
                    res.send({
                        msg: "添加成功!",
                        code: 200
                    });
                }
            }
        });
    }
});
// 寻物搜索
router.get('/searchTitle', function (req, res) {
    console.log(req.query.searchTitle);
    var sql = `select * from takelist where takeTitle  REGEXP ?`
    db(sql, [req.query.searchTitle], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
module.exports = router;